﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FitnessDiary.WebUI.Models;
using FitnessDiary.Domain.Abstract;
using FitnessDiary.WebUI.Account;
using FitnessDiary.Domain.Entities;
using FitnessDiary.WebUI.Filters;

namespace FitnessDiary.WebUI.Controllers
{
	[InitializeSimpleMembership]
	[Authorize]
	public class HitController : Controller
	{
		IFitnessDiaryRepository repository;
		IUserProvider account;

		public HitController(IFitnessDiaryRepository repository, IUserProvider account)
		{
			this.repository = repository;
			this.account = account;
		}

		public ActionResult Index(int id)
		{
			Hit hit = repository.Hits.Where(h => h.Id == id).Where(h => h.Stat.UserId == account.UserId).FirstOrDefault();
			if (hit == null)
			{
				return HttpNotFound();
			}
			hit.HitComponents = repository.HitComponents.Where(h => h.Hit.Id == id).ToList();
			return View(hit);
		}

		public ActionResult List()
		{
			HitModel model = new HitModel();
			model.Hits = repository.Hits.Where(h => h.Stat.UserId == account.UserId).ToList();
			foreach (Hit hit in model.Hits)
			{
				hit.HitComponents = repository.HitComponents.Where(h => h.Hit.Id == hit.Id).ToList();
			}
			return View(model);
		}

		public ActionResult Stat(int id)
		{
			HitModel model = new HitModel();
			model.Hits = repository.Hits.Where(h => h.Stat.Id == id).Where(h => h.Stat.UserId == account.UserId).ToList();
			if (model == null)
			{
				return HttpNotFound();
			}
			foreach (Hit hit in model.Hits)
			{
				hit.HitComponents = repository.HitComponents.Where(h => h.Hit.Id == hit.Id).ToList();
			}
			return View("List", model);
		}

		public ActionResult StatOnDay(int stat, DateTime date)
		{
			HitModel model = new HitModel();
			model.Hits = repository.Hits.Where(h => h.Date == date).Where(h => h.Stat.Id == stat).Where(h => h.Stat.UserId == account.UserId).ToList();
			if (model == null)
			{
				return HttpNotFound();
			}
			foreach (Hit hit in model.Hits)
			{
				hit.HitComponents = repository.HitComponents.Where(h => h.Hit.Id == hit.Id).ToList();
			}
			model.Date = date;
			return View(model);
		}

		public ActionResult Day(DateTime date)
		{
			HitModel model = new HitModel();
			model.Hits = repository.Hits.Where(h => h.Date == date).Where(h => h.Stat.UserId == account.UserId).ToList();
			if (model == null)
			{
				return HttpNotFound();
			}
			ViewData["StatList"] = new SelectList(repository.Stats.Where(s => s.UserId == account.UserId).ToList(), "Id", "Name");
			foreach (Hit hit in model.Hits)
			{
				hit.HitComponents = repository.HitComponents.Where(h => h.Hit.Id == hit.Id).ToList();
			}
			model.Date = date;
			return View(model);
		}

		public ActionResult Create(DateTime date, int stat)
		{
			Stat statToModel = repository.Stats.Where(s => s.Id == stat).Where(s => s.UserId == account.UserId).FirstOrDefault();
			statToModel.Quantities = repository.Quantities.Where(q => q.Stat.Id == stat).ToList();
			if (statToModel == null)
			{
				return HttpNotFound();
			}
			Hit hit = new Hit
			{
				Stat = statToModel,
				Date = date
			};
			return View(hit);
		}

		[HttpPost]
		public ActionResult Create(Hit hit, int stat)
		{
			ModelState.Remove("Stat");
			Stat statToModel = repository.Stats.Where(s => s.Id == stat).Where(s => s.UserId == account.UserId).FirstOrDefault();
			List<Quantity> quantitiesOfStat = repository.Quantities.Where(q => q.Stat.Id == stat).ToList();
			statToModel.Quantities = quantitiesOfStat;
			hit.Stat = statToModel;
			if (ModelState.IsValid)
			{
				for (int i = 0; i < statToModel.Quantities.Count; i++)
				{
					hit.HitComponents[i].Quantity = quantitiesOfStat[i];
				}
				repository.SaveHit(hit);
				return RedirectToAction("Index", "Diary");
			}
			return View(hit);
		}

		public ActionResult Delete(int id)
		{
			Hit hit = repository.Hits.Where(h => h.Id == id).Where(h => h.Stat.UserId == account.UserId).FirstOrDefault();
			if (hit == null)
			{
				return HttpNotFound();
			}
			return View(hit);
		}

		[HttpPost, ActionName("Delete")]
		public ActionResult DeleteConfirmed(int id)
		{
			Hit model = repository.Hits.Where(h => h.Id == id).Where(h => h.Stat.UserId == account.UserId).FirstOrDefault();
			if (model == null)
			{
				return HttpNotFound();
			}
			model.HitComponents = repository.HitComponents.Where(h => h.Hit.Id == id).ToList();
			repository.DeleteHit(model);
			return RedirectToAction("Index", "Diary");
		}
	}
}
